package com.carleasoft.mps.core.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author taoyujing
 * @date 2021-12-11 15:05
 * @project server
 */

public class ListUtil {

    //相对均匀
    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remaider = source.size() % n;  //(先计算出余数)
        int number = source.size() / n;  //然后是商
        //如果没有余数则均分
        if (remaider != 0) {
            number++;
        }
        int remainder = source.size() % number;

        for (int i = 0; i < n; i++) {
            List<T> ts = null;
            if (i < n - 1 || remaider == 0) {
                ts = source.subList(i * number, (i + 1) * number);
            } else {//当有余数时，最后一组去余数长度
                ts = source.subList(source.size() - remainder, source.size());
            }
            result.add(ts);
        }
        return result;
    }

}
